<template>
  <div class="createPost-container">
    <sticky :z-index="10" :class-name="'sub-navbar '">
      <el-button v-if="false" v-loading="loading" style="margin-left: 10px;" type="success" @click="AddNewLine(true)" :disabled="list2.length > 0 || contraryU_BOUDF == ''">
        添加反面数据（复制正面数据）
      </el-button>
      <el-button v-if="false" v-loading="loading" style="margin-left: 10px;" type="success" @click="GetFromCookie()">
        草稿Get
      </el-button>
      <el-button v-if="hasCache" v-loading="loading" style="margin-left: 10px;" type="danger" @click="SaveToCookie()">
        草稿存储
      </el-button>
      <el-button v-if="list2.length == 0 && contraryU_BOUDF != ''" v-loading="loading" style="margin-left: 10px;" type="success" @click="AddNewLine(false)" :disabled="list2.length > 0">
        添加反面数据
      </el-button>
      <el-button v-loading="loading" type="warning" @click="backtoPreviousTags(false)">
        返回
      </el-button>
    </sticky>

<!--    <el-table v-show="false" v-loading="listLoading" :data="list" border highlight-current-row style="overflow-x: auto;">
      <el-table-column align="center" fixed :label="contraryU_BOUDF!='' ? '正面单据号':'单据号'" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.DocEntry }}</span>
        </template>
      </el-table-column>

      <el-table-column v-for="item in cols" align="center" :label="item.U_Fd + (item.U_IsReq == '1' ? ' *' : '')" width="150" :label-class-name="item.U_IsReq == '1' ? 'column-style' : ''" >
        <template slot-scope="scope">
          <template v-if="scope.row.edit">
            <el-input v-if="item.U_FdType == 'A' || item.U_FdType == 'M'" v-model="scope.row[item.U_Fd]" type="text" class="edit-input" size="small" />
            <el-input v-else-if="item.U_FdType == 'B' || item.U_FdType == 'N'" v-model="scope.row[item.U_Fd]" @input="limitInputDecim($event, item.U_Fd, item.U_Decim)" type="number" class="edit-input" size="small" />
            <el-input v-else v-model="scope.row[item.U_Fd]" class="edit-input" size="small" />
          </template>
          <span v-else>{{ scope.row[item.U_Fd] }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" label="操作" min-width="200" style="display: flex;">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check-outline"
            @click="confirmEdit(scope.row)"
          >
            Ok
          </el-button>
          <el-button
            v-if="scope.row.edit"
            class="cancel-btn"
            size="small"
            type="warning"
            @click="cancelEdit(scope.row)"
          >
            cancel
          </el-button>
          <el-button
            v-else
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="scope.row.edit=!scope.row.edit"
          >
            Edit
          </el-button>
          <el-button
            v-if="list2.length == 0 && contraryU_BOUDF != '' && !scope.row.edit"
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="AddNewLine"
          >
            Add
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-table v-show="false" v-loading="listLoading" v-if="list2.length>0" :data="list2" border highlight-current-row style="overflow-x: auto;">
      <el-table-column align="center" fixed label="反面单据号" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.DocEntry }}</span>
        </template>
      </el-table-column>

      <el-table-column v-for="item in cols2" align="center" :label="item.U_Fd.replace(contraryU_BOUDF,'') + (item.U_IsReq == '1' ? ' *' : '')" width="150" :label-class-name="item.U_IsReq == '1' ? 'column-style' : ''" >
        <template slot-scope="scope">
          <template v-if="scope.row.edit">
            <el-input v-if="item.U_FdType == 'A' || item.U_FdType == 'M'" v-model="scope.row[item.U_Fd]" type="text" class="edit-input" size="small" />
            <el-input v-else-if="item.U_FdType == 'B' || item.U_FdType == 'N'" v-model="scope.row[item.U_Fd]" @input="limitInputDecim($event, item.U_Fd, item.U_Decim)" type="number" class="edit-input" size="small" />
            <el-input v-else v-model="scope.row[item.U_Fd]" class="edit-input" size="small" />
          </template>
          <span v-else>{{ scope.row[item.U_Fd] }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" label="操作" min-width="200" style="display: flex;">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check-outline"
            @click="confirmSecondRowEdit(scope.row)"
          >
            Ok
          </el-button>
          <el-button
            v-if="scope.row.edit"
            class="cancel-btn"
            size="small"
            type="warning"
            @click="cancelSecondRowEdit(scope.row)"
          >
            cancel
          </el-button>
          <el-button
            v-else
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="scope.row.edit=!scope.row.edit"
          >
            Edit
          </el-button>
        </template>
      </el-table-column>
    </el-table>
 -->
    <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> -->


    <div v-loading="listLoading" style="display: flex;margin-bottom: 40px;margin-left:20px;">
      <div>
        <div style='text-align: center; color: #606266; padding: 10px 0; font-weight: 600; font-size: 16px;'>正 面</div>
        <el-form ref="form" :model="postForm" label-width="160px" style="width:400px;">
          <el-form-item label="产品">
            <span style="color:red;width:20px;margin-left:-5px;"></span>
            <el-input disabled :value="U_ItemCode"></el-input>
          </el-form-item>
          <el-form-item label="批号">
            <span style="color:red;width:20px;margin-left:-5px;"></span>
            <el-input disabled :value="lot_id"></el-input>
          </el-form-item>
          <el-form-item v-for="item in cols" :label="item.U_Fd">
            <span style="color:red;width:20px;margin-left:-5px;">{{item.U_IsReq == '1' ? ' *' : ' '}}</span>
            <el-input v-if="item.U_FdType == 'B' || item.U_FdType == 'N'" v-model="postForm[item.U_Fd]" type='number' @input="limitInputDecim($event, item.U_Fd, item.U_Decim)"></el-input>
            <el-input v-else-if="item.U_FdType == 'A'" v-model="postForm[item.U_Fd]" type='string'></el-input>
<!--            <el-input v-else-if="item.U_FdType == 'D'" v-model="postForm[item.U_Fd]" type='date'></el-input> -->
            <el-input v-else v-model="postForm[item.U_Fd]" ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
          </el-form-item>

        </el-form>
      </div>
      <div v-loading="listLoading" v-if="list2.length>0" style="margin-left:20px;">
        <div style='text-align: center; color: #606266; padding: 10px 0; font-weight: 600; font-size: 16px;'>反 面</div>
        <el-form ref="form" :model="contraryPostForm" label-width="160px" style="width:400px;">
          <el-form-item label="产品">
            <span style="color:red;width:20px;margin-left:-5px;"></span>
            <el-input disabled :value="U_ItemCode"></el-input>
          </el-form-item>
          <el-form-item label="批号">
            <span style="color:red;width:20px;margin-left:-5px;"></span>
            <el-input disabled :value="lot_id"></el-input>
          </el-form-item>
          <el-form-item v-for="item in cols2" :label="item.U_Fd.replace(contraryU_BOUDF,'')">
            <span style="color:red;width:20px;margin-left:-5px;">{{item.U_IsReq == '1' ? ' *' : ' '}}</span>
            <el-input v-if="item.U_FdType == 'B' || item.U_FdType == 'N'" v-model="contraryPostForm[item.U_Fd]" type='number' @input="limitInputDecim($event, item.U_Fd, item.U_Decim)"></el-input>
            <el-input v-else-if="item.U_FdType == 'A'" v-model="contraryPostForm[item.U_Fd]" type='string'></el-input>
            <!-- <el-input v-else-if="item.U_FdType == 'D'" v-model="contraryPostForm[item.U_Fd]" type='date'></el-input> -->
            <el-input v-else v-model="contraryPostForm[item.U_Fd]" ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmitContrary">提交</el-button>
          </el-form-item>

        </el-form>
      </div>
    </div>

  </div>
</template>

<script>
import axios from 'axios'
import Sticky from '@/components/Sticky'
import { deepClone, limitInputNumber } from '@/utils'
import { fetchBOUDD1s, updateBOUDDs, GetBouddDocEntryByRefCode, GetBouddDocEntryByRefEntry, fetchContraryMLUDByU_BOUDF, fetchContraryBOUDD1s } from '@/api/res'
import Cookies from 'js-cookie'
import dbls from '@/utils/localStorage'

const defaultForm = {
  status: 'draft',
  Enable: true,
  display_time: undefined, // 前台展示时间
  id: undefined,
  platforms: ['a-platform'],
  comment_disabled: false
}

export default {
  name: 'MpcpendDetail',
  components: {
    Sticky
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      postForm: {},
      contraryPostForm: {},
      loading: false,
      listLoading:false,
      list: [],
      list2: [],
      dialogVisible: false,
      listQuery: {
        page: 1,
        limit: 20
      },
      cols:[],
      cols2:[],
      id:'',
      U_BOUDF:'',
      U_RefCode:'',
      U_RefEntry:'',
      tempRoute: {},
      contraryU_BOUDF:'',
      U_ItemCode:'',
      lot_id:'',
      hasCache:false
    }
  },
  computed: {
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      const U_BOUDF = this.$route.query && this.$route.query.U_BOUDF
      const U_RefEntry = this.$route.query && this.$route.query.U_RefCode

      this.U_ItemCode = this.$route.query && decodeURIComponent(this.$route.query.U_ItemCode)
      this.lot_id = this.$route.query && decodeURIComponent(this.$route.query.lotid)

      //console.log('MpcpendDetail---created------',U_ItemCode,lot_id)

      this.U_BOUDF = U_BOUDF
      //console.log('created----',U_RefEntry, U_RefEntry == undefined, U_RefEntry == 'undefined')

      fetchContraryMLUDByU_BOUDF({'U_BOUDF':U_BOUDF}).then(resData=>{
        console.log('fetchContraryMLUDByU_BOUDF------', resData)
        this.contraryU_BOUDF = resData

        if(U_RefEntry == undefined){
          GetBouddDocEntryByRefEntry({'U_RefEntry':id,'U_BOUDF':U_BOUDF}).then(res=>{
            console.log(`GetBouddDocEntryByRefEntry-----condition:U_RefEntry = ${id} & U_BOUDF = ${U_BOUDF},Result:${res}`)
            if(res != ''){
              this.U_RefEntry = id
              this.id = res
              this.getList({
                U_BOUDF : U_BOUDF,
                DocEntry : res,
              });
            }else{
              this.U_RefEntry = id
              this.id = ''
              this.getList({
                U_BOUDF : U_BOUDF,
                DocEntry : '9999999'
              });
            }
          })
        }else{
          this.U_RefEntry = U_RefEntry
          this.id = id
          this.getList({
            U_BOUDF : U_BOUDF,
            DocEntry : id
          });
        }
      })

    }

    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    GetFromCookie(){
      //dbls.remove('mpcpend')
      var _cache = dbls.get('mpcpend')
      var result = null
      if(!(_cache instanceof Array)){
        result = []
      }else{
        result = _cache.filter(v=>v.docEntry == this.U_RefEntry && v.U_BOUDF == this.U_BOUDF)
      }

      if(result.length>0){
        this.postForm = result[0].postForm

        if(Object.keys(result[0].contraryPostForm).length>0){
          this.AddNewLine()
          this.contraryPostForm = result[0].contraryPostForm
        }
      }

      console.log('GetFromCookie---------',this.U_RefEntry,_cache,result[0],this.list2)
      //console.log('GetFromCookie--SessionKey-------',Cookies.get('SessionKey'))
    },
    DeleteFromCookie(){
      var _cache = dbls.get('mpcpend')
      _cache = _cache.filter(item=>!(item.docEntry == this.U_RefEntry && item.U_BOUDF == this.U_BOUDF))
      dbls.save('mpcpend',_cache)
    },
    SaveToCookie(){
      var _cache = dbls.get('mpcpend')
      var _item = { docEntry: this.U_RefEntry, U_BOUDF: this.U_BOUDF, postForm: this.postForm, contraryPostForm: this.contraryPostForm }

      if(!(_cache instanceof Array)){
        dbls.save('mpcpend',[_item])
      }else{
        var flag = false
        _cache = _cache.map(item=>{
          if(item.docEntry == this.U_RefEntry && item.U_BOUDF == this.U_BOUDF){
            item.postForm = this.postForm
            item.contraryPostForm = this.contraryPostForm
            flag = true
          }
          return item
        })

        if(!flag){
          _cache.push(_item)
        }

        dbls.save('mpcpend',_cache)
      }
      
      this.$message({ message: '草稿保存成功', type: 'success' })
      console.log('SaveToCookie---------',_cache)
    },
    onSubmit(){
      console.log('onSubmit-postForm--------',this.postForm,this.list)
      var row = this.postForm
      const cols = this.cols.map(v=>v.U_Fd)
      var list = []
      var _checkFlag = false
      this.cols.forEach(item=>{

        if(item.U_IsReq == '1' && row[item.U_Fd].trim() == ''){
          this.$notify({ title: '提示', message: '['+item.U_Fd + '] 内容不能为空，请重新输入！', type: 'warning', duration: 4000 })
          row[item.U_Fd] = row['original'+item.U_Fd]
          _checkFlag = true
        }

      })

      if(_checkFlag){
        return
      }

        this.cols.forEach(item=>{
          if((item.U_FdType == 'B' || item.U_FdType == 'N') && row[item.U_Fd].trim() == ''){
            row[item.U_Fd] = 0
          }
          row['original'+item.U_Fd] = row[item.U_Fd]
          list.push({'U_Fd':item.U_Fd, 'U_FdType' :item.U_FdType,'val': row[item.U_Fd], 'LineId': item.U_LineId })
        })

        console.log('confirmEdit-----',row)

        const config ={
          DocEntry : row.DocEntry == '1+' ? '' : row.DocEntry,
          U_RefEntry : this.U_RefEntry,
          U_BOUDF : this.U_BOUDF,
          U_RefType : 'T'+this.U_BOUDF,
          list : list
        }

        //{"DocEntry":"","U_RefCode":"51","U_BOUDF":"tbsc","list":[{"U_Fd":"涂布头间隙","U_FdType":"N","val":"30"},{"U_Fd":"涂胶实际宽度","U_FdType":"N","val":"35"},{"U_Fd":"原材料批号","U_FdType":"A","val":"AA2301"},{"U_Fd":"机速","U_FdType":"B","val":"15"},{"U_Fd":"贴合温度","U_FdType":"B","val":"120"},{"U_Fd":"贴合压力","U_FdType":"B","val":"75"},{"U_Fd":"烘箱1温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱2温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱3温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱4温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱5温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱6温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱7温度","U_FdType":"B","val":"121"},{"U_Fd":"放料张力","U_FdType":"B","val":"55"},{"U_Fd":"放料储料架张力","U_FdType":"B","val":"55"},{"U_Fd":"烘箱张力","U_FdType":"B","val":"55"},{"U_Fd":"收料摆动轮张力","U_FdType":"B","val":"55"},{"U_Fd":"收料张力","U_FdType":"B","val":"55"},{"U_Fd":"贴膜储料架张力","U_FdType":"B","val":"55"},{"U_Fd":"贴膜张力","U_FdType":"B","val":"56"},{"U_Fd":"剥离收卷张力","U_FdType":"B","val":"54"},{"U_Fd":"备注","U_FdType":"A","val":"test"}]}

        console.log('confirmEdit--1----',config,JSON.stringify(config))

        updateBOUDDs(config).then(res=>{
          console.log('confirmEdit--ok----',config, res)
          row.DocEntry = res.data.data
          this.DeleteFromCookie()
          this.$message({ message: '操作成功', type: 'success' })
          this.backtoPreviousTags(true)

        }).catch(err=>{
          console.log('confirmEdit--err----',err)
        })
    },
    onSubmitContrary(){
      console.log('onSubmitcontraryPostForm--------',this.contraryPostForm)
      var row = this.contraryPostForm
      const cols = this.cols2.map(v=>v.U_Fd)
      var list = []
      var _checkFlag = false
      this.cols2.forEach(item=>{

        if(item.U_IsReq == '1' && row[item.U_Fd].trim() == ''){
          this.$notify({ title: '提示', message: '['+item.U_Fd + '] 内容不能为空，请重新输入！', type: 'warning', duration: 4000 })
          row[item.U_Fd] = row['original'+item.U_Fd]
          _checkFlag = true
        }

      })

      if(_checkFlag){
        return
      }

        this.cols2.forEach(item=>{
          if((item.U_FdType == 'B' || item.U_FdType == 'N') && row[item.U_Fd].trim() == ''){
            row[item.U_Fd] = 0
          }
          row['original'+item.U_Fd] = row[item.U_Fd]
          list.push({'U_Fd':item.U_Fd.replace(this.contraryU_BOUDF,''), 'U_FdType' :item.U_FdType,'val': row[item.U_Fd], 'LineId': item.U_LineId })
        })

        console.log('confirmSecondRowEdit-----',row)

        const config ={
          DocEntry : row.DocEntry == '2+' ? '' : row.DocEntry,
          U_RefEntry : this.U_RefEntry,
          U_BOUDF : this.U_BOUDF,
          U_RefType : this.contraryU_BOUDF,
          list : list
        }

        //{"DocEntry":"","U_RefCode":"51","U_BOUDF":"tbsc","list":[{"U_Fd":"涂布头间隙","U_FdType":"N","val":"30"},{"U_Fd":"涂胶实际宽度","U_FdType":"N","val":"35"},{"U_Fd":"原材料批号","U_FdType":"A","val":"AA2301"},{"U_Fd":"机速","U_FdType":"B","val":"15"},{"U_Fd":"贴合温度","U_FdType":"B","val":"120"},{"U_Fd":"贴合压力","U_FdType":"B","val":"75"},{"U_Fd":"烘箱1温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱2温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱3温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱4温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱5温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱6温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱7温度","U_FdType":"B","val":"121"},{"U_Fd":"放料张力","U_FdType":"B","val":"55"},{"U_Fd":"放料储料架张力","U_FdType":"B","val":"55"},{"U_Fd":"烘箱张力","U_FdType":"B","val":"55"},{"U_Fd":"收料摆动轮张力","U_FdType":"B","val":"55"},{"U_Fd":"收料张力","U_FdType":"B","val":"55"},{"U_Fd":"贴膜储料架张力","U_FdType":"B","val":"55"},{"U_Fd":"贴膜张力","U_FdType":"B","val":"56"},{"U_Fd":"剥离收卷张力","U_FdType":"B","val":"54"},{"U_Fd":"备注","U_FdType":"A","val":"test"}]}

        console.log('confirmSecondRowEdit--1----',config,JSON.stringify(config))

        updateBOUDDs(config).then(res=>{
          console.log('confirmSecondRowEdit--ok----',config, res)
          row.DocEntry = res.data.data
          this.DeleteFromCookie()
          this.$message({
            message: 'The data has been edited',
            type: 'success'
          })

          this.backtoPreviousTags(true)

        }).catch(err=>{
          console.log('confirmSecondRowEdit--err----',err)
        })
    },
    dis(val,d2){
      console.log('dis------',val,d2)
    },

    limitInputDecim(value, name, decim) {
      this.list[0][name] = limitInputNumber(value, name, decim)
    },

    AddNewLine(hasCopy){
      var _row = JSON.parse(JSON.stringify(this.list[0]))
      var _cols2 = JSON.parse(JSON.stringify(this.cols))
      var _newrow = {}
      var _code = ''
      _cols2 = _cols2.map(v=>{
        v.Code = this.contraryU_BOUDF
        v.DocEntry = '2+'
        v.U_Fd += v.Code
        _code = v.Code

        return v
      })

      var _base = JSON.parse(JSON.stringify(this.list[0]))
      Object.keys(_row).forEach(key => {
       var _col = key+_code
        _newrow[_col]= hasCopy ? _base[key] :""
      })

      console.log('base-----',_base,_newrow)

       _newrow.DocEntry = "2+"
       _newrow.edit = false

      this.cols2 = _cols2
      this.contraryPostForm = _newrow
      this.list2.push(_newrow)
    },

    cancelEdit(row) {
      console.log('cancelEdit--0----',row)
      const cols = this.cols.map(v=>v.U_Fd)
      cols.forEach(item=>{
        row[item] = row['original'+item]
      })

      console.log('cancelEdit--1----',row)
      row.edit = false
      this.$message({
        message: 'The data has been restored to the original value',
        type: 'warning'
      })
    },
    confirmEdit(row) {
      console.log('confirmEdit--0----',this.cols)
      row.edit = false
      const cols = this.cols.map(v=>v.U_Fd)
      var list = []
      this.cols.forEach(item=>{

        if(item.U_IsReq == '1' && row[item.U_Fd].trim() == ''){
          this.$notify({ title: '提示', message: '['+item.U_Fd + '] 内容不能为空，请重新输入！', type: 'warning', duration: 4000 })
          row[item.U_Fd] = row['original'+item.U_Fd]
          return false
        }

      })

        this.cols.forEach(item=>{
          if((item.U_FdType == 'B' || item.U_FdType == 'N') && row[item.U_Fd].trim() == ''){
            row[item.U_Fd] = 0
          }
          row['original'+item.U_Fd] = row[item.U_Fd]
          list.push({'U_Fd':item.U_Fd, 'U_FdType' :item.U_FdType,'val': row[item.U_Fd], 'LineId': item.U_LineId })
        })

        console.log('confirmEdit-----',row)

        const config ={
          DocEntry : row.DocEntry == '1+' ? '' : row.DocEntry,
          U_RefEntry : this.U_RefEntry,
          U_BOUDF : this.U_BOUDF,
          U_RefType : 'T'+this.U_BOUDF,
          list : list
        }

        //{"DocEntry":"","U_RefCode":"51","U_BOUDF":"tbsc","list":[{"U_Fd":"涂布头间隙","U_FdType":"N","val":"30"},{"U_Fd":"涂胶实际宽度","U_FdType":"N","val":"35"},{"U_Fd":"原材料批号","U_FdType":"A","val":"AA2301"},{"U_Fd":"机速","U_FdType":"B","val":"15"},{"U_Fd":"贴合温度","U_FdType":"B","val":"120"},{"U_Fd":"贴合压力","U_FdType":"B","val":"75"},{"U_Fd":"烘箱1温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱2温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱3温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱4温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱5温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱6温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱7温度","U_FdType":"B","val":"121"},{"U_Fd":"放料张力","U_FdType":"B","val":"55"},{"U_Fd":"放料储料架张力","U_FdType":"B","val":"55"},{"U_Fd":"烘箱张力","U_FdType":"B","val":"55"},{"U_Fd":"收料摆动轮张力","U_FdType":"B","val":"55"},{"U_Fd":"收料张力","U_FdType":"B","val":"55"},{"U_Fd":"贴膜储料架张力","U_FdType":"B","val":"55"},{"U_Fd":"贴膜张力","U_FdType":"B","val":"56"},{"U_Fd":"剥离收卷张力","U_FdType":"B","val":"54"},{"U_Fd":"备注","U_FdType":"A","val":"test"}]}

        console.log('confirmEdit--1----',config,JSON.stringify(config))

        updateBOUDDs(config).then(res=>{
          console.log('confirmEdit--ok----',config, res)
          row.DocEntry = res.data.data
          this.$message({
            message: 'The data has been edited',
            type: 'success'
          })
        }).catch(err=>{
          console.log('confirmEdit--err----',err)
        })
    },
    cancelSecondRowEdit(row) {
      console.log('cancelSecondRowEdit--0----',row)
      const cols = this.cols2.map(v=>v.U_Fd)
      cols.forEach(item=>{
        row[item] = row['original'+item]
      })

      console.log('cancelSecondRowEdit--1----',row)
      row.edit = false
      this.$message({
        message: 'The data has been restored to the original value',
        type: 'warning'
      })
    },
    confirmSecondRowEdit(row) {
      console.log('confirmSecondRowEdit--0----',this.cols2)
      row.edit = false
      const cols = this.cols2.map(v=>v.U_Fd)
      var list = []
      this.cols2.forEach(item=>{

        if(item.U_IsReq == '1' && row[item.U_Fd].trim() == ''){
          this.$notify({ title: '提示', message: '['+item.U_Fd + '] 内容不能为空，请重新输入！', type: 'warning', duration: 4000 })
          row[item.U_Fd] = row['original'+item.U_Fd]
          return false
        }

      })

        this.cols2.forEach(item=>{
          if((item.U_FdType == 'B' || item.U_FdType == 'N') && row[item.U_Fd].trim() == ''){
            row[item.U_Fd] = 0
          }
          row['original'+item.U_Fd] = row[item.U_Fd]
          list.push({'U_Fd':item.U_Fd.replace(this.contraryU_BOUDF,''), 'U_FdType' :item.U_FdType,'val': row[item.U_Fd], 'LineId': item.U_LineId })
        })

        console.log('confirmSecondRowEdit-----',row)

        const config ={
          DocEntry : row.DocEntry == '2+' ? '' : row.DocEntry,
          U_RefEntry : this.U_RefEntry,
          U_BOUDF : this.U_BOUDF,
          U_RefType : this.contraryU_BOUDF,
          list : list
        }

        //{"DocEntry":"","U_RefCode":"51","U_BOUDF":"tbsc","list":[{"U_Fd":"涂布头间隙","U_FdType":"N","val":"30"},{"U_Fd":"涂胶实际宽度","U_FdType":"N","val":"35"},{"U_Fd":"原材料批号","U_FdType":"A","val":"AA2301"},{"U_Fd":"机速","U_FdType":"B","val":"15"},{"U_Fd":"贴合温度","U_FdType":"B","val":"120"},{"U_Fd":"贴合压力","U_FdType":"B","val":"75"},{"U_Fd":"烘箱1温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱2温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱3温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱4温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱5温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱6温度","U_FdType":"B","val":"120"},{"U_Fd":"烘箱7温度","U_FdType":"B","val":"121"},{"U_Fd":"放料张力","U_FdType":"B","val":"55"},{"U_Fd":"放料储料架张力","U_FdType":"B","val":"55"},{"U_Fd":"烘箱张力","U_FdType":"B","val":"55"},{"U_Fd":"收料摆动轮张力","U_FdType":"B","val":"55"},{"U_Fd":"收料张力","U_FdType":"B","val":"55"},{"U_Fd":"贴膜储料架张力","U_FdType":"B","val":"55"},{"U_Fd":"贴膜张力","U_FdType":"B","val":"56"},{"U_Fd":"剥离收卷张力","U_FdType":"B","val":"54"},{"U_Fd":"备注","U_FdType":"A","val":"test"}]}

        console.log('confirmSecondRowEdit--1----',config,JSON.stringify(config))

        updateBOUDDs(config).then(res=>{
          console.log('confirmSecondRowEdit--ok----',config, res)
          row.DocEntry = res.data.data
          this.$message({
            message: 'The data has been edited',
            type: 'success'
          })
        }).catch(err=>{
          console.log('confirmSecondRowEdit--err----',err)
        })
    },

    getList(row){
      this.listLoading = true
      var that = this
      fetchBOUDD1s({
        page : this.listQuery.page,
        limit : this.listQuery.limit,
        U_BOUDF : row.U_BOUDF,
        DocEntry : row.DocEntry == '9999999' ? '1+' : row.DocEntry,
        U_RefEntry : this.U_RefEntry
      }).then(res => {
        const {cols, contents} = res
        //this.list = contents
        this.cols = cols

        this.list = contents.map(v => {
          this.$set(v, 'edit', false)

          cols.forEach(item=>{
            v['original'+item.U_Fd] = v[item.U_Fd]
          })

          return v
        })

        this.hasCache = row.DocEntry == '9999999' ? true : false

        this.postForm = this.list[0]

        if(this.list[0].DocEntry == '1+'){
          this.hasCache = true
        }

        console.log('fetchBOUDD1s ok---------',res,this.list)

        if(this.contraryU_BOUDF != ''){
          fetchContraryBOUDD1s({
            page : this.listQuery.page,
            limit : this.listQuery.limit,
            U_BOUDF : row.U_BOUDF,
            DocEntry : row.DocEntry == '9999999' ? '2+' : row.DocEntry,
            U_RefEntry : this.U_RefEntry,
            contraryU_BOUDF : this.contraryU_BOUDF,
          }).then(res => {
              const {cols, contents} = res
              if(contents.length == 0){
                this.cols2 = []
                this.list2 = []
                this.listLoading = false
              }else{
                this.list2 = contents.map(v => {
                  this.$set(v, 'edit', false)
                  return v
                })
                this.cols2 = cols
                this.listLoading = false
                this.contraryPostForm = this.list2[0]
                console.log('fetchContraryBOUDD1s--1------',cols,contents)
              }

              this.GetFromCookie()

          }).catch(err=>{console.log(err)})
        }
        else{
          this.cols2 = []
          this.list2 = []
          this.GetFromCookie()
          this.listLoading = false
        }

        that.setTagsViewTitle()
        that.setPageTitle()

      }).catch(err=>{
        console.log('fetchMPCP1s err---------',err)
      })
    },

    setTagsViewTitle() {
      const title = 'LOT数据编辑'
      const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.id}` })
      this.$store.dispatch('tagsView/updateVisitedView', route)
    },
    setPageTitle() {
      const title = 'LOT数据编辑'
      document.title = `${title} - ${this.id}`
    },

    backtoPreviousTags(reflush) {
      const title = 'LOT数据编辑'
      const route = Object.assign({}, this.tempRoute, {
        title: `${title}-${this.postForm.id}`
      })
      this.$store.dispatch('tagsView/delView', route).then(({
        visitedViews
      }) => {
        const latestView = visitedViews.slice(-1)[0]
        console.log('latestView--------------', latestView)
        if (latestView) {

          if (reflush && reflush == true) {
            //this.$router.go(0)
            this.$router.push(latestView.fullPath+'?dt='+new Date().getTime())
          }else{
            this.$router.push(latestView.fullPath)
          }
        } else {
          if (view.name === 'Dashboard') {
            this.$router.replace({
              path: '/redirect' + view.fullPath
            })
          } else {
            this.$router.push('/')
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}

.el-form-item {
    margin-bottom: 5px;
}

// .avatar{
//   width: 200px;
//   height: 200px;
//   border-radius: 50%;
// }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<style lang="scss">
  .el-table .column-style {
    color: red;
  }

  .el-form-item__content{
    display: flex;
  }

</style>
